package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;

import java.util.ArrayList;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.UserGridViewBinding;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/9 10:03
 * description:我的宫格适配器
 */
public class UserGridAdapter extends DelegateAdapter.Adapter<UserGridAdapter.UserGridHolder> {

    private Context mContext;
    private ArrayList<String> mList = new ArrayList<>();
    private ArrayList<Integer> iconList = new ArrayList<>();

    private LayoutHelper mGridLayoutHelper;
    private String orderExtent;

    public UserGridAdapter(Context context, GridLayoutHelper gridLayoutHelper) {
        mContext = context;
        mGridLayoutHelper = gridLayoutHelper;

        mList.add("商城");
        mList.add("文化");
        mList.add("旅游");
        mList.add("体育");
        iconList.add(R.drawable.user_coupon_sc);
        iconList.add(R.drawable.user_coupon_wh);
        iconList.add(R.drawable.user_coupon_ly);
        iconList.add(R.drawable.user_coupon_ty);
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.user_grid_view;
    }


    @Override
    public int getItemCount() {
        return null == mList ? 0 : mList.size();
    }

    @Override
    public UserGridHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return UserGridHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(UserGridHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(final int position, UserGridViewBinding binding) {
        binding.userGridIcon.setImageResource(iconList.get(position));
        binding.userGridTv.setText(mList.get(position));
        binding.userGridLl.setOnClickListener(new View.OnClickListener() {
            String access_token = ACache.get(mContext).getAsString("access_token");

            @Override
            public void onClick(View v) {
//
                if (position == 3) {
                    if (access_token == null) {
                        toLogin();
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", Constant.TYHM_URL + "portal/my_hxcode.html?" + orderExtent);
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                    }
                }
                if (position == 2) {
                    if (access_token == null) {
                        toLogin();
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString(Constant.TITLE, "优惠券");
                        L.e(Constant.TAG, "access_token====" + access_token);
                        bundle.putString("url", Constant.BASE_URL + "app-front-end/#/mine/coupons?access_token=" + access_token + "&ntype=" + "1");
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

//
                    }
                }
                if (position == 1) {
                    //文化
                    if (access_token == null) {
                        toLogin();
                    } else {
                        Bundle bundle = new Bundle();
                        String test = Constant.WHXF_URL + "quan?id=&" + Constant.EXTENT;
                        bundle.putString("url", test);
                        bundle.putString(Constant.TITLE, "");
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                    }
                }
                if (position == 0) {
                    //商城
                    if (access_token == null) {
                        toLogin();
                    } else {
                        Bundle bundle = new Bundle();
                        L.e(Constant.TAG, "access_token====" + access_token);
                        bundle.putString("url", Constant.BASE_URL + "app-front-end/#/mine/coupons?access_token=" + access_token + "&ntype=" + "2");
                        bundle.putString(Constant.TITLE, "");
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                    }
                }
            }
        });

    }

    public void toLogin() {
        JumpUtils.jump(mContext, "login", 0);
    }

    public void setOrderExtent(String orderExtent) {
        this.orderExtent = orderExtent;
    }

    static class UserGridHolder extends RecyclerView.ViewHolder {

        public UserGridViewBinding binding;

        public static UserGridHolder creat(ViewGroup parent) {
            UserGridViewBinding binding = UserGridViewBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new UserGridHolder(binding);
        }

        public UserGridHolder(UserGridViewBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
